package com.hs.biye.controller.admin;

import com.hs.biye.constant.JwtClaimConstant;
import com.hs.biye.constant.MessageConstant;
import com.hs.biye.model.DTO.EmployeeRequest;
import com.hs.biye.model.VO.EmployeeVO;
import com.hs.biye.model.entity.Employee;
import com.hs.biye.model.enums.GenderEnum;
import com.hs.biye.model.properties.JwtProperties;
import com.hs.biye.model.reslut.Result;
import com.hs.biye.model.reslut.ResultPage;
import com.hs.biye.service.DepartmentService;
import com.hs.biye.service.EmployeeService;
import com.hs.biye.utils.JwtUtil;
import com.hs.biye.utils.WiteListUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/emp")
@Slf4j
public class EmployeeController {

    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private EmployeeService employeeService;

    @Resource
    private DepartmentService departmentService;

    /**
     * 登录
     *
     * @param employeeRequest
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeVO> login(@RequestBody EmployeeRequest employeeRequest) {
        log.info("员工登录：{}", employeeRequest);
        if (employeeRequest == null) {
            Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        Employee employee = employeeService.login(employeeRequest);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimConstant.ADM_ID, employee.getEmployeeId());

        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        //用户登录了将token存放带登录白名单里
        WiteListUtils.add(token);
        log.info("白名单_登录:{}", WiteListUtils.witeList);

        EmployeeVO employeeVO = EmployeeVO.builder()
                .employeeId(employee.getEmployeeId())
                .employeeName(employee.getEmployeeName())
                .roler(employee.getRoler())
                .token(token)
                .build();
        return Result.success(employeeVO);
    }


    /**
     * 退出
     *
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/logout")
    public Result<Boolean> logout(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("Token");
        log.info("白名单_退出1:{}", WiteListUtils.witeList);
        boolean remove = WiteListUtils.remove(token);
        log.info("白名单_退出2:{}", WiteListUtils.witeList);
        return Result.success(remove);
    }


    /**
     * 获取单个员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getone/{id}")
    public Result<EmployeeVO> getEmployeeOne(@PathVariable Integer id) {
        if (id == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        EmployeeVO employeeVO = employeeService.getOneEmployee(id);
        if (employeeVO == null) {
            return Result.faile(MessageConstant.NOT_QUERY);
        }
        return Result.success(employeeVO);
    }

    @GetMapping("/getNameId/{id}")
    public Result< List<EmployeeVO>> getEmployeeNotRoom(@PathVariable Integer id) {
        if (id == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        List<EmployeeVO> res = employeeService.getEmployeeNotRoom(id);
        return Result.success(res);
    }

    @GetMapping("/getNameId2/{id}/{id2}")
    public Result< List<EmployeeVO>> getEmployeeNotRoom2(@PathVariable Integer id,@PathVariable Integer id2) {
        if (id == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        List<EmployeeVO> res = employeeService.getEmployeeNotRoom2(id,id2);
        return Result.success(res);
    }


    /**
     * 分页查询
     * @param employeeRequest
     * @return
     */
    @PostMapping("/getpages")

    public ResultPage<EmployeeVO> getEmployees(@RequestBody EmployeeRequest employeeRequest) {
        if (employeeRequest == null) {
            return ResultPage.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        ResultPage<EmployeeVO> employee = employeeService
                .getEmployee(employeeRequest.getPages(), employeeRequest.getSize(), employeeRequest);
        if (employee == null) {
            return ResultPage.faile(MessageConstant.NOT_QUERY);
        }
        return employee;
    }

    /**
     * 添加员工
     *
     * @param employeeRequest
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> addEmployee(@RequestBody EmployeeRequest employeeRequest) {
        if (employeeRequest == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeRequest, employee);
        employee.setGender(GenderEnum.getCodeByValue(employeeRequest.getGender()));
        employee.setEmployeeId(null);
        //默认密码
        String password = DigestUtils.md5DigestAsHex(MessageConstant.DEFAULT_PASSWORD.getBytes());
        employee.setPassword(password);
        boolean b = employeeService.addEmployee(employee);
        return Result.success(b);
    }

    /**
     * x
     * 更新员工
     *
     * @param employeeRequest
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> updateEmployee(@RequestBody EmployeeRequest employeeRequest) {
        if (employeeRequest == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeRequest, employee);

        //设置性别对应的数字标识
        employee.setGender(GenderEnum.getCodeByValue(employeeRequest.getGender()));


        boolean b = employeeService.updateEmployee(employee);
        return Result.success(b);
    }


    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteEmployee(@PathVariable Integer id) {
        if (id == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        boolean b = employeeService.removeEmployee(id);
        return Result.success(b);
    }

    @GetMapping("/getebyd/{id}")
    public Result<List<EmployeeVO>> getEmployeeByDepId(@PathVariable Integer id) {
        if (id == null) {
            return Result.faile(MessageConstant.PAR_NOT_RIGHT);
        }
        List<EmployeeVO> res = employeeService.getEmployeeByDepId(id);
        return Result.success(res);
    }
}
